package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.merchant.dao.AuthDao;
import cc.linker.steplink.crm.merchant.pojo.FreightTemplatePo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Map;
import java.util.*;

@Service
public class AuthService {

    @Autowired
    private AuthDao authDao;

    public Map<String, Object> getAuthInfo(String orgId) {
        Map<String, Object> map = authDao.getInfo(orgId);
        if (map == null) {
            this.insertAuth(orgId);
            map = authDao.getInfo(orgId);
        }
        return map;
    }

    public void insertAuth(String orgId){
        Map<String, Object> insertMap = new HashMap<>();
        Integer tsgOpen = 0;
        Integer cusOpen = 0;
        Integer marOpen = 0;
        Integer epaOpen = 0;
        Integer payMode = authDao.getPayModeByOrgId(orgId);
        if(payMode!=null && payMode!=3){
            tsgOpen = 1;
            cusOpen = 1;
            marOpen = 1;
            epaOpen = 1;
        }
        insertMap.put("orgId",orgId);
        insertMap.put("tsgOpen",tsgOpen);
        insertMap.put("cusOpen",cusOpen);
        insertMap.put("marOpen",marOpen);
        insertMap.put("epaOpen",epaOpen);
        authDao.insert(insertMap);
    }

    public void saveAuth(Map<String, Object> map) {
        authDao.update(map);
    }

    /**
     * 获取商家列表数据
     */
    public PageInfo<Map<String, Object>> getSalerList(Integer pageIndex, Integer pageSize,String account,String orgId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Map<String, Object>> pageList = authDao.getSalerList(account,orgId);
        return new PageInfo<>(pageList);
    }


    //文章考核、AI营销机器人、AI客户专员、传播溯源、旺旺直播、供货商
    public List<Map<String, Object>> getApplicationList() {
        List<Map<String, Object>> applicationList = authDao.getApplicationList();
        if (null == applicationList || applicationList.size() == 0) {
            if(null == applicationList){
                applicationList = new ArrayList<>();
            }
            Map<String,Object> app1 = new HashMap<>();
            app1.put("id", UUID.randomUUID().toString());
            app1.put("applicationName", "文章考核");
            app1.put("applicationImg", "");
            app1.put("applicationDesc", "");
            app1.put("applicationNum", 1);
            app1.put("applicationDetail", "");
            app1.put("applicationAlias", "wzkh");
            applicationList.add(app1);
            authDao.addApplication(app1);

            Map<String,Object> app2 = new HashMap<>();
            app2.put("id", UUID.randomUUID().toString());
            app2.put("applicationName", "AI营销机器人");
            app2.put("applicationImg", "");
            app2.put("applicationDesc", "");
            app2.put("applicationNum", 1);
            app2.put("applicationDetail", "");
            app2.put("applicationAlias", "yxjqr");
            applicationList.add(app2);
            authDao.addApplication(app2);

            Map<String,Object> app3 = new HashMap<>();
            app3.put("id", UUID.randomUUID().toString());
            app3.put("applicationName", "AI客户专员");
            app3.put("applicationImg", "");
            app3.put("applicationDesc", "");
            app3.put("applicationNum", 1);
            app3.put("applicationDetail", "");
            app3.put("applicationAlias", "khzy");
            applicationList.add(app3);
            authDao.addApplication(app3);

            Map<String,Object> app4 = new HashMap<>();
            app4.put("id", UUID.randomUUID().toString());
            app4.put("applicationName", "传播溯源");
            app4.put("applicationImg", "");
            app4.put("applicationDesc", "");
            app4.put("applicationNum", 1);
            app4.put("applicationDetail", "");
            app4.put("applicationAlias", "cbsy");
            applicationList.add(app4);
            authDao.addApplication(app4);

            Map<String,Object> app5 = new HashMap<>();
            app5.put("id", UUID.randomUUID().toString());
            app5.put("applicationName", "旺旺直播");
            app5.put("applicationImg", "");
            app5.put("applicationDesc", "");
            app5.put("applicationNum", 1);
            app5.put("applicationDetail", "");
            app5.put("applicationAlias", "wwzb");
            applicationList.add(app5);
            authDao.addApplication(app5);

            Map<String,Object> app6 = new HashMap<>();
            app6.put("id", UUID.randomUUID().toString());
            app6.put("applicationName", "供货商");
            app6.put("applicationImg", "");
            app6.put("applicationDesc", "");
            app6.put("applicationNum", 1);
            app6.put("applicationDetail", "");
            app6.put("applicationAlias", "ghs");
            applicationList.add(app6);
            authDao.addApplication(app6);
        }
        return applicationList;
    }

    public Map<String, Object> getApplicationInfo(String id) {
        return authDao.getApplicationInfo(id);
    }

    public void saveApplication(Map<String, Object> map) {
        if(!map.containsKey("id") || StringUtils.isEmpty(map.get("id"))){
            throw GlobalException.build("参数错误");
        }
        authDao.saveApplication(map);
    }

    public Integer getSalerCount(String userId) {
        return authDao. getSalerCount(userId);
    }

    public String getOrgIdByUserId(String userId) {
        return authDao. getOrgIdByUserId(userId);
    }

    public Integer getIsOpen(Map map) {
        return authDao. getIsOpen(map);
    }

    public Map<String, Object> getApplicationInfoByAlias(String applicationAlias) {
        return authDao. getApplicationInfoByAlias(applicationAlias);
    }
}
